package sctu.edu.java.year2016.team02.project.pc.UI;

import sctu.edu.java.year2016.team02.project.pc.problem.PcProblem;
import sctu.edu.java.year2016.team02.project.pc.problem.SocketListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

/**
 * Created by Eoly on 2016/6/12.
 */
//管理员对问题加以回答所用的界面
public class AnswerUI extends JDialog implements ActionListener{
    private JTextArea answer;
    private Button submit;
    private String key;
    private JScrollPane scro;
    public AnswerUI(JDialog parent, String title,boolean modal,String key,Object[] list) {
        super(parent, title, modal);
        this.key = key;
        answer = new JTextArea((String)list[4]);
        scro = new JScrollPane(answer);
        submit = new Button("Submit");
        setLayout(null);
        submit.setBounds(210, 220, 80, 25);
        submit.addActionListener(this);
        scro.setBounds(10,10,465,200);
        answer.setLineWrap(true);
        add(submit);
        add(scro);
        setBounds(SocketListener.screenWidth / 3, SocketListener.ScreenHeight / 4, 500, 300);
        setResizable(false);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==submit){
            if(!"".equals(answer.getText())){
                try {
                    PcProblem.update("Problem", "SolveResult", key, "KeyToRE", answer.getText());
                    PcProblem.update("Problem","SolvedOrNot", key,"KeyToRE", 1);
                    JOptionPane.showMessageDialog(this, "提交成功", "提示", JOptionPane.PLAIN_MESSAGE);
                    dispose();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }else JOptionPane.showMessageDialog(this, "提交内容不能为空", "提示", JOptionPane.PLAIN_MESSAGE);
        }
    }
}
